package pub.tbc.toolkit.web.config;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pub.tbc.toolkit.Loops;
import pub.tbc.toolkit.exception.GlobalException;
import pub.tbc.toolkit.function.Consumer;

import java.io.File;

/**
 * 仅适用于文件夹结构
 *
 * @author tbc tianbencai@e-eduspace.com
 * @version 1.0 {2016年5月18日 下午3:37:10}
 */
//@Component
public class PropertiesScan {
    private static Logger log = LoggerFactory.getLogger(PropertiesScan.class);

    public static void scan() throws RuntimeException {
        File file = new File(PropertiesScan.class.getClassLoader().getResource("").getPath());
        File[] files = file.listFiles();
        Loops.forEach(files, new Consumer<File>() {
            @Override
            public void accept(File f) {
                if (f.isDirectory())
                    return;
                if (!f.getName().endsWith(".properties"))
                    return;
                PropertiesConfiguration config = null;
                try {
                    config = new PropertiesConfiguration(f);
                } catch (ConfigurationException e) {
                    log.error("读取配置文件失败： {}", e.getMessage());
                    throw new RuntimeException("[扫描器]读取配置文件失败");
                }
                Config.add(f.getName().replace(".properties", "").toLowerCase(), config);
            }
        });
    }

}
